<?php
/**
 * Created by PhpStorm
 * User: ZhouFurong
 * Date: 2021/1/28
 * Time: 10:33
 */


$param = [

    ['id' => '11111', 'account' => 201, 'user' => '1'],
    ['id' => '22222', 'account' => 201, 'user' => '1'],
    ['id' => '121212', 'account' => 201, 'user' => '1'],
    ['id' => '33333', 'account' => 203, 'user' => '1'],
    ['id' => '44444', 'account' => 203, 'user' => '1'],
    ['id' => '88888', 'account' => 203, 'user' => '1'],
    ['id' => '141414', 'account' => 203, 'user' => '1'],
    ['id' => '55555', 'account' => 204, 'user' => '2'],
    ['id' => '66666', 'account' => 204, 'user' => '2'],
    ['id' => '77777', 'account' => 204, 'user' => '2'],
    ['id' => '13131', 'account' => 204, 'user' => '2'],
    ['id' => '55555', 'account' => 205, 'user' => '2'],
    ['id' => '66666', 'account' => 205, 'user' => '2'],
    ['id' => '34344', 'account' => 205, 'user' => '2'],
    ['id' => '45454', 'account' => 205, 'user' => '2'],

];

$param = [
    '1' => [
        '201' => [
            ['id' => '11111', 'account' => 201, 'user' => '1'],
            ['id' => '22222', 'account' => 201, 'user' => '1'],
            ['id' => '121212', 'account' => 201, 'user' => '1'],
        ],
        '203' => [
            ['id' => '33333', 'account' => 203, 'user' => '1'],
            ['id' => '44444', 'account' => 203, 'user' => '1'],
            ['id' => '88888', 'account' => 203, 'user' => '1'],
            ['id' => '141414', 'account' => 203, 'user' => '1'],
        ]
    ],
    '2' => [
        '204' => [
            ['id' => '55555', 'account' => 204, 'user' => '1'],
            ['id' => '66666', 'account' => 204, 'user' => '1'],
            ['id' => '77777', 'account' => 204, 'user' => '1'],
            ['id' => '13131', 'account' => 204, 'user' => '1'],
        ],
        '205' => [
            ['id' => '55555', 'account' => 205, 'user' => '1'],
            ['id' => '66666', 'account' => 205, 'user' => '1'],
            ['id' => '34344', 'account' => 205, 'user' => '1'],
            ['id' => '45454', 'account' => 205, 'user' => '1'],
        ],
    ]
];

foreach ($param as $user => $value) {
    $array = getUserData($value);
    echo PHP_EOL . '用户' . $user . PHP_EOL;
    var_export($array);
}


function getUserData($datas)
{
    $array = [];
    $sum = 0;
    $accountArr = array_keys($datas);
    getMYData($datas, $accountArr, $array, $sum);
    return $array;
}

function getMYData(&$datas, $accountArr, &$array, &$sum)
{
    $time = time();
    $count = count($accountArr);
    //echo PHP_EOL . 'account count ' . $count . PHP_EOL;
    if ($count == 0) {
        return;
    }

    for ($i = 0; $i <= $count - 1; $i++) {
        ++$sum;
        $data = array_pop($datas[$accountArr[$i]]);
        // echo PHP_EOL.'推送数据 '.PHP_EOL;
        //  var_export($data);
        if ($data) {
            $data['executing_time_sort'] = $time + $sum;
            // 加队列跑
            $array[] = $data;
            unset($data);
        }
        if (empty($datas[$accountArr[$i]])) {
            //  echo PHP_EOL . $i . ' 账号没有数据unset 掉 ' . PHP_EOL;
            unset($datas[$accountArr[$i]]);
            unset($accountArr[$i]);
            continue;
        }


    }

    // echo PHP_EOL.'打印数据 '.PHP_EOL;
    // var_export($array);
    if (count($datas)) {
        sort($accountArr);
        //   var_export($accountArr);
        //    echo PHP_EOL.'账号有数据 递归'.PHP_EOL;
        getMYData($datas, $accountArr, $array, $sum);
    }

}
